package com.yang.modules.common.advice;

import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;

/**
 * 记录Feign调用的耗时
 *
 * @author: Billyang
 * @date: 2024/9/21 15:04
 */
@Slf4j
@Aspect
@Component
public class FeignCostTimeAspect {

    @Around("@within(org.springframework.cloud.openfeign.FeignClient)")
    public Object feignCostTime(ProceedingJoinPoint joinPoint) throws Throwable {
        long start = System.currentTimeMillis();
        Object result = joinPoint.proceed();
        try {
            long end = System.currentTimeMillis();
            Signature signature = joinPoint.getSignature();
            log.info("request feign:{},mills:{},args:{},result:{}", signature.toShortString().replace("(..)", ""), end - start,
                    JSON.toJSONString(joinPoint.getArgs()), JSON.toJSONString(result));
        } catch (Exception e) {
            log.error("request feign error", e);
        }
        return result;
    }
}
